//
//  SideBarTableViewController.m
//  WEIMO
//
//  Created by Lixiaoyong on 13-6-13.
//  Copyright (c) 2013年 广州从兴电子开发有限公司. All rights reserved.
//

#import "SideBarTableViewController.h"

#import "JSONKit.h"
#import "DLog.h"
#import "FMManager.h"

#define kCellHeight (self.view.frame.size.height / 5)

@interface SideBarTableViewController ()
@property (nonatomic,retain) NSMutableArray *slideBarDatas;     //<KeyValueObject>
@property (nonatomic,copy) NSString *lastClickedSlideItem;      //记住上次的选择
@end

@implementation SideBarTableViewController
@synthesize slideBarDatas;
@synthesize lastClickedSlideItem;
@synthesize delegate;

- (void)dealloc
{
    self.slideBarDatas = nil;
    self.lastClickedSlideItem = nil;
    self.delegate = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self)
    {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:40/255.0 green:40/255.0 blue:40/255.0 alpha:1.0];
    self.tableView.separatorColor = [UIColor clearColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.lastClickedSlideItem = @"";
    
    //获取数据
    [self getSlideBarDatasFromLocalJsonFile];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - private methods

//获取侧边栏数据
//"status","message","array"
//"selfId","parentId","name","image","selectedImage"
- (void)getSlideBarDatasFromLocalJsonFile
{
    NSMutableArray *slideDatas = [[NSMutableArray alloc] init];
    NSMutableArray *menuDatas = [FMManager getMenuDatasOfChoosedFirstMenu:@"0"];
    if (menuDatas.count == 0)
    {
        NSString *file = [[NSBundle mainBundle] pathForResource:@"mode_sidebar" ofType:@"json"];
        NSString *string = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
        NSDictionary *result = [string objectFromJSONString];
        NSString *status = [result objectForKey:@"status"];
        if ([status isEqualToString:@"200"])
        {
            NSArray *array = [result objectForKey:@"array"];
            menuDatas = [NSMutableArray arrayWithArray:array];
        }
    }
    
    int tag = 0;
    for (int i=0; i<menuDatas.count; i++)
    {
        NSDictionary *dic = [menuDatas objectAtIndex:i];
        NSString *parentId = [dic objectForKey:@"parentId"];
        if ([parentId isEqualToString:@"0"])//找出一级目录
        {
            NSString *name = [dic objectForKey:@"name"];
            tag ++ ;
            KeyValueObject *item = [[KeyValueObject alloc] init];
            item.key = [dic objectForKey:@"selfId"];
            item.value = [NSString stringWithFormat:@"%d",tag];
            item.keyDispName = name;
            item.valueDispName = [dic objectForKey:@"image"];
            item.others = [dic objectForKey:@"selectedImage"];
            [slideDatas addObject:item];
            DLog(@"name:%@,image:%@,tag:%@",item.keyDispName,item.valueDispName,item.value);
            [item release];
        }
    }
    
    //将slideDatas每两个为一组分为一个array
    self.slideBarDatas = [[[NSMutableArray alloc] init] autorelease];
    int number = 2;
    int count = slideDatas.count;
    int rows = count / number;
    int lasts = count % number;
    for (int i=0; i<=rows; i++)
    {
        NSMutableArray *oneRows = [[NSMutableArray alloc] init];
        for (int j=0; j<number; j++)
        {
            if (i==rows && j==lasts)
            {
                return;
            }
            
            KeyValueObject *item = [slideDatas objectAtIndex:(i*number + j)];
            [oneRows addObject:item];
        }
        
        if (oneRows.count > 0)
        {
            [self.slideBarDatas addObject:oneRows];
        }
        [oneRows release];
    }
    [slideDatas release];
    
    [self.tableView reloadData];
    
    DLog(@"slideBarDatas:%d",self.slideBarDatas.count);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.slideBarDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *slideBarCellIdentifier = @"Cell";
    SlideBarCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:slideBarCellIdentifier];
    if (cell == nil)
    {
        cell = [[[SlideBarCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:slideBarCellIdentifier] autorelease];
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    CGSize size = CGSizeMake(tableView.frame.size.width, kCellHeight);
    NSArray *array = [self.slideBarDatas objectAtIndex:indexPath.row];
    [cell addDataToCell:array withSize:size];
    
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kCellHeight;
}

#pragma mark - SlideBarCustomCellDelegate

- (void)clickedSlideBarCustomCellItem:(SlideBarItemButton *)slideButton
{
    DLog(@"clicked:%d,%@,%@,%@",slideButton.tag,slideButton.item.keyDispName,slideButton.item.key,slideButton.item.value);
    
    NSString *flag = slideButton.item.value;
    if ([self.lastClickedSlideItem isEqualToString:@""])
    {
        self.lastClickedSlideItem = flag;
    }
    
    [slideButton refreshButtonState:YES];
    
    if (![flag isEqualToString:self.lastClickedSlideItem])
    {
        int tag = self.lastClickedSlideItem.intValue;
        SlideBarItemButton *button = (SlideBarItemButton *)[self.view viewWithTag:tag];
        [button refreshButtonState:NO];
        self.lastClickedSlideItem = flag;
    }
    
    if (self.delegate)
    {
        [self.delegate clickedSlideBarOneItem:slideButton.item];
    }
}

@end